<?php
/********************************************************************
Product    : Plotalot
Date       : 15 March 2011
Copyright  : Les Arbres Design 2011
Contact    : http://extensions.lesarbresdesign.info
Licence    : GNU General Public License
Description: Displays a list of charts on the front end
*********************************************************************/
defined('_JEXEC') or die('Restricted Access'); 

// load the Plotalot class file

require_once JPATH_ADMINISTRATOR.'/components/com_plotalot/helpers/plotalot.php';

// get parameters from the active menu item

	$app = JFactory::getApplication('site');
	$menu_params =  & $app->getParams();
	if ($menu_params == null)
		{
		echo 'Menu item not found';
		return;
		}
		
// display the heading and top text	

	echo '<h2>'.$menu_params->get('page_hdr').'</h2>';
	echo $menu_params->get('top_text');
	echo '<br />';

// draw the charts

	$chart_id_array = explode(",",$menu_params->get('chart_ids'));
	
	$plotalot = new Plotalot;

	foreach ($chart_id_array as $chart_id)
		{
		$plot_info = loadChart($chart_id);
		if ($plot_info === null)
			{
			echo "Chart $chart_id not defined<br />";
			continue;
			}
		$chart = $plotalot->drawChart($plot_info);

		if ($chart == '')
			echo "Chart $chart_id : ".$plotalot->error."<br />";
		else
			echo $chart;
		}
		
// display the bottom text		

	echo '<br />';
	echo $menu_params->get('bottom_text');

	return;

//-------------------------------------------------------------------------------
// Load a chart definition and expand the plots field
//
function loadChart($id)
{
	$db = &JFactory::getDBO();
	$query = "SELECT * FROM #__plotalot WHERE id = $id";
	$db->setQuery($query);
	$data = $db->loadObject();
	$plots = stripslashes($data->plots);
	$params = explode("\n",$plots);	// expand the plots field
	foreach ($params as $param)
		{
		$p = strpos($param,"=");
		if ($p === false)
			continue;				// should never happen
		$key = substr($param,0,$p);
		$value = substr($param,$p+1);
		$field_num = substr($key,5);
		$q = strrpos($field_num,"_");
		$plot_num = substr($field_num,$q+1);
		$field_name = substr($field_num,0,$q);
		$data->plot_array[$plot_num][$field_name] = $value;
		}	
	return $data;
}
?>
